package xyz.naokeziteng.data_structure.bst;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * @author hawk
 * @date 2022/8/25
 * @desc 二分搜索树
 **/
public class BST {

    private TreeNode root;
    private int size;

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    //向二分搜索树中添加新的元素
    public void add(int e) {
        root = add(root, e);
    }

    //向以Node为根的二分搜索树中添加元素e,递归算法
    //返回插入新节点后的根
    private TreeNode add(TreeNode node, int e) {
        if (node == null) {
            size++;
            return new TreeNode(e);
        }

        if (node.val < e) {
            node.right = add(node.right, e);
        } else if (node.val > e) {
            node.left = add(node.left, e);
        }
        return node;
    }

    //非递归
    private TreeNode add2(TreeNode node, int e) {
        size++;
        if (node == null) {
            return new TreeNode(e);
        }

        TreeNode tmp = node;
        while (true) {
            if (tmp.val == e) {
                size--;
                break;
            }
            if (tmp.val > e && tmp.left == null) {
                tmp.left = new TreeNode(e);
                break;
            } else if (tmp.val < e && tmp.right == null) {
                tmp.right = new TreeNode(e);
                break;
            }
            tmp = tmp.val > e ? tmp.left : tmp.right;
        }
        return node;
    }


    public boolean contains(int e) {
        return contains(root, e);
    }

    private boolean contains(TreeNode node, int e) {
        if (node == null) {
            return false;
        }

        if (node.val == e) {
            return true;
        } else if (node.val > e) {
            return contains(node.left, e);
        } else {
            return contains(node.right, e);
        }
    }

    public TreeNode search(int e) {
        return search(root, e);
    }

    private TreeNode search(TreeNode node, int e) {
        if (node == null) {
            return null;
        }

        if (node.val == e) {
            return node;
        } else if (node.val > e) {
            return search(node.left, e);
        } else {
            return search(node.right, e);
        }
    }

    //前序遍历
    private void preOrder(TreeNode node) {
        if (node == null) {
            return;
        }

        System.out.println(node.val);
        preOrder(node.left);
        preOrder(node.right);
    }

    //前序遍历，非递归
    private void preOrder2(TreeNode node) {
        if (node == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(node);
        while (!stack.isEmpty()) {
            TreeNode cur = stack.pop();
            System.out.println(cur.val);
            if (cur.right != null) {
                stack.push(cur.right);
            }
            if (cur.left != null) {
                stack.push(cur.left);
            }
        }

    }

    //中序遍历
    private void inOrder(TreeNode node) {
        if (node == null) {
            return;
        }
        inOrder(node.left);
        System.out.println(node.val);
        inOrder(node.right);
    }

    //后序遍历
    private void postOrder(TreeNode node) {
        if (node == null) {
            return;
        }
        postOrder(node.left);
        postOrder(node.right);
        System.out.println(node.val);
    }

    //层序遍历
    private void levelOrder(TreeNode node) {
        if (node == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(node);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.remove();
            System.out.println(cur.val);
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }

    // 寻找二分搜索树的最小元素
    public int minmum() {
        if (size == 0) {
            throw new IllegalArgumentException("BST is empty!");
        }
        return minmum(root).val;
    }

    //返回以node为根的二分搜索树的最小值所在的节点
    private TreeNode minmum(TreeNode node) {
        if (node.left == null) {
            return node;
        }

        return minmum(node.left);
    }

    //删除最小节点，返回最小值
    public int removeMin() {
        int min = minmum();
        root = removeMin(root);
        return min;
    }

    //删除最小节点
    //返回删除后的根节点
    private TreeNode removeMin(TreeNode node) {
        if (node.left == null) {
            TreeNode rightNode = node.right;
            node.right = null;
            size--;
            return rightNode;
        }
        node.left = removeMin(node.left);
        return node;
    }

    // 寻找二分搜索树的最大元素
    public int maxmum() {
        if (size == 0) {
            throw new IllegalArgumentException("BST is empty!");
        }
        return maxmum(root).val;
    }

    //返回以node为根的二分搜索树的最大值所在的节点
    private TreeNode maxmum(TreeNode node) {
        if (node.right == null) {
            return node;
        }

        return minmum(node.right);
    }

    //删除最大节点，返回最大值
    public int removeMax() {
        int maxmum = maxmum();
        root = removeMax(root);
        return maxmum;
    }

    private TreeNode removeMax(TreeNode node) {
        if (node.right == null) {
            TreeNode leftNode = node.left;
            node.left = null;
            size--;
            return leftNode;
        }
        node.right = removeMax(node.right);
        return node;
    }

    //删除任意节点
    public void remove(int e) {
        root = remove(root, e);
    }

    //删除任意节点，分三种情况
    //被删除节点左子树为空，右子树直接替换
    //被删除节点右子树为空，左子树直接替换
    //被删除节点左右子树都不为空,有两种做法，找到右子树中的最小值或者左子树中的最大值，来替换被删除节点
    private TreeNode remove(TreeNode node, int e) {
        if (node == null) {
            return null;
        }
        if (node.val < e) {
            node.right = remove(node.right, e);
        } else if (node.val > e) {
            node.left = remove(node.left, e);
        } else {
            if (node.left == null) {
                TreeNode rightNode = node.right;
                node.right = null;
                size--;
                return rightNode;
            } else if (node.right == null) {
                TreeNode leftNode = node.left;
                node.left = null;
                size--;
                return leftNode;
            } else {
                //找到右子树最小值
                TreeNode rightMin = minmum(node.right);
                //删除右子树最小值
                node.right = removeMin(node.right);
                rightMin.left = node.left;
                rightMin.right = node.right;
                node = rightMin;
            }
        }
        return node;
    }


    //树节点
    private class TreeNode {
        //值
        private int val;
        //左子树
        private TreeNode left;
        //右子树
        private TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }

        public int getVal() {
            return val;
        }

        public void setVal(int val) {
            this.val = val;
        }

        public TreeNode getLeft() {
            return left;
        }

        public void setLeft(TreeNode left) {
            this.left = left;
        }

        public TreeNode getRight() {
            return right;
        }

        public void setRight(TreeNode right) {
            this.right = right;
        }
    }

    //最大深度
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return Math.max(left, right) + 1;
    }

    public static void main(String[] args) {
        BST bst = new BST();
        bst.add(5);
        bst.add(2);
        bst.add(7);
        bst.add(1);
        bst.add(4);
        bst.add(6);
        bst.add(9);
        bst.add(8);
        bst.levelOrder(bst.root);
        System.out.println("节点数量: " + bst.size);
        bst.remove(7);
        System.out.println("===========");
        bst.levelOrder(bst.root);
        System.out.println("节点数量: " + bst.size);
        bst.remove(2);
        System.out.println("===========");
        bst.levelOrder(bst.root);
        System.out.println("节点数量: " + bst.size);

    }
}
